﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BySoft.Server.Consts;
using BySoft.Server.Business;
using BySoft.Web.Utility;
using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Enum;
using OneSoft.EntityCore;
using BySoft.Web.Controls;

namespace BySoft.Web.Site.SysManage
{
    public partial class RoleList : AuthBasePage
    {
        int m_count = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region 判断用户权限

                BindData(0);
                #endregion
            }
        }

        private void BindData(int pageIndex)
        {
            DataTable dtData = null;
            if (CurrentUser.UserID == CommonConsts.ADMIN_USER ||CurrentUser.UserID =="ByAdmin")//只有ADMIN超级管理员可以修改用户类型对应的默认权限
                dtData = SysRole.GetRoleList(txtRoleName.Text.Trim(), pageIndex, pcData.PageSize, out m_count);
            else
                dtData = SysRole.GetRoleListWithoutUserTypeRole(txtRoleName.Text.Trim(), pageIndex, pcData.PageSize, out m_count);
            pcData.RecordCount = m_count;
            if (dtData.Rows.Count > 0)
            {
                grdResult.DataSource = dtData;
                grdResult.DataBind();


                pcData.CurrentIndex = pageIndex;
                //pcData.Visible = true;
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dtData);
                //pcData.Visible = false;
            }
        }


        protected void pcData_PageIndexClick(object sender, PageIndexClickEventArgs e)
        {
            BindData(e.PageIndex);
        }

        public string SetFormat(object param)
        {
            return HttpUtility.UrlEncode(param.ToString());
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData(0);
        }

        protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                #region 删除绑定
                ImageButton ibtn = (ImageButton)e.Row.FindControl("ibtnDel");
                Button btnDel = (Button)e.Row.FindControl("btnDel");
                if (ibtn != null)
                {
                    ibtn.OnClientClick = "ShowTopConfirm('提示信息','" + Message.ConfirmDelete + "','confirmCallback(\\'" + btnDel.ClientID + "\\')');return false;";
                }
                #endregion

                if (grdResult.DataKeys[e.Row.RowIndex].Values["ROLEID"].ToString() == CommonConsts.Admin_Role_ID) //该行为超级管理员
                {
                    if (CurrentUser.UserID != CommonConsts.ADMIN_USER && CurrentUser.UserID != "ByAdmin")//ADMIN超级管理员不检查权限
                    {
                        if (ConfigurationManager.AppSettings["Right_Enable"].ToString().Equals("1"))//权限是否开启
                        {
                            DataTable dtUserRole = SysUserrole.GetRoleByUser(CurrentUser.UserID);
                            //if (dtUserRole.Select(SysRoleTable.C_ROLEID + "='" + CommonConsts.Admin_Role_ID + "'").Length == 0) //数据可以选择全部的超级管理员不检查权限
                            //{
                            ((System.Web.UI.HtmlControls.HtmlImage)e.Row.FindControl("imgEdit")).Visible = false;
                            //}
                        }
                    }
                    ((System.Web.UI.WebControls.ImageButton)e.Row.FindControl("ibtnDel")).Visible = false;
                }
            }
        }

        protected void grdResult_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                int roleId = CommonMethod.ConvertToInt(e.CommandArgument);

                if (SysRole.IsContainUser(roleId.ToString()))
                {
                    UIHelper.AlertMessage(this.Page, Message.RoleContainUser);
                    return;
                }
                SysRoleEntity entity = SysRole.GetSysRoleByID(roleId);
                if (SysRole.DeleteSysRole(entity))
                {
                    UIHelper.AlertMessage(this.Page, Message.DeleteSuccess);
                    BindData(0);
                    base.LogAction(BaseEnum.ActionType.Delete, entity.RoleID.Value.ToString(), true, null, new EntityBase[] { entity });
                }
                else
                {
                    UIHelper.AlertMessage(this.Page, Message.DeleteFail);
                    base.LogAction(BaseEnum.ActionType.Delete, entity.RoleID.Value.ToString(), false, null, new EntityBase[] { entity });
                }
            }
        }
    }
}
